package com.bhs.sansonglogistics.ui.mine;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bhs.sansonglogistics.R;
import com.bhs.sansonglogistics.base.BaseActivity;
import com.bhs.sansonglogistics.bean.RevenueBean;
import com.bhs.sansonglogistics.net.NetCallBack;
import com.bhs.sansonglogistics.ui.order.OrderSearchActivity;
import com.bhs.sansonglogistics.utils.DateUtils;
import com.bhs.sansonglogistics.utils.LogUtils;
import com.google.gson.Gson;
import com.loper7.date_time_picker.DateTimeConfig;
import com.loper7.date_time_picker.dialog.CardDatePickerDialog;

import java.util.Calendar;

import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * 我的营收
 */
public class MyRevenueActivity extends BaseActivity implements View.OnClickListener, NetCallBack {


    private TextView mTvHaveAlreadySettled;
    private TextView mTvUnliquidated;
    private TextView mTvOrderDetails;
    private TextView mTvDataUpdateTime;
    private RadioButton mRbNearlyDay7;
    private RadioButton mRbNearlyDay30;
    private TextView mTvAccomplish;
    private TextView mTvUnfinished;
    private long currentTimestamp;
    private RadioGroup mRgNearly;
    private TextView mTvGeneralIncome;
    private TextView mTvDataUpdateTime2;
    private TextView mTvYearMonth;
    private long theSameMonth;

    @Override
    public int initLayout() {
        return R.layout.activity_my_revenue;
    }

    @Override
    public void initView() {
        TextView mTvTitle = findViewById(R.id.tv_title);
        mTvTitle.setText("我的营收");

        mTvHaveAlreadySettled = findViewById(R.id.tv_have_already_settled);
        mTvUnliquidated = findViewById(R.id.tv_unliquidated);
        mTvOrderDetails = findViewById(R.id.tv_order_details);
        mTvDataUpdateTime = findViewById(R.id.tv_data_update_time);
        mRbNearlyDay7 = findViewById(R.id.rb_nearly_day7);
        mRbNearlyDay30 = findViewById(R.id.rb_nearly_day30);
        mTvAccomplish = findViewById(R.id.tv_accomplish);
        mTvUnfinished = findViewById(R.id.tv_unfinished);

        mTvOrderDetails.setOnClickListener(this);
        currentTimestamp = System.currentTimeMillis();
        theSameMonth = System.currentTimeMillis();
        mRgNearly = findViewById(R.id.rg_nearly);
        findViewById(R.id.ll_have_already_settled).setOnClickListener(this);
        findViewById(R.id.ll_unsettled).setOnClickListener(this);
        mTvGeneralIncome = findViewById(R.id.tv_general_income);
        mTvDataUpdateTime2 = findViewById(R.id.tv_data_update_time2);
        mTvYearMonth = findViewById(R.id.tv_year_month);
        mTvYearMonth.setOnClickListener(this);
    }

    @Override
    public void initData() {

        Calendar calendar = Calendar.getInstance();
        //年
        int year = calendar.get(Calendar.YEAR);
        //月
        int month = calendar.get(Calendar.MONTH) + 1;
        mTvYearMonth.setText(String.format("%s-%s", year, month));
        mTvDataUpdateTime.setText(String.format("(数据更新至%s)", DateUtils.getYearMonthDay(currentTimestamp)));
        mTvDataUpdateTime2.setText(String.format("(数据更新至%s)", DateUtils.getYearMonthDay(currentTimestamp)));
        loadingPopupShow();
        networkRequest(netApi.my_income_1(DateUtils.getMonthBegin(currentTimestamp),
                DateUtils.getMonthEnd(currentTimestamp)), this, 999);
        networkRequest(netApi.my_income_2("7"), this, 888);
        mRgNearly.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                if (i == R.id.rb_nearly_day7) {
                    networkRequest(netApi.my_income_2("7"), MyRevenueActivity.this, 888);
                } else {
                    networkRequest(netApi.my_income_2("30"), MyRevenueActivity.this, 888);
                }
            }
        });
    }

    CardDatePickerDialog datePickerDialog;

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.tv_year_month) {
            if (datePickerDialog == null) {
                datePickerDialog = new CardDatePickerDialog.Builder(mActivity)
                        .setTitle("选择时间")
                        .showBackNow(false)
                        .setDisplayType(DateTimeConfig.YEAR, DateTimeConfig.MONTH)
                        .showFocusDateInfo(false)
                        .setThemeColor(Color.parseColor("#485EF4"))
                        .setOnChoose("确定", aLong -> {
                            theSameMonth = aLong;
                            mTvYearMonth.setText(DateUtils.getYearMonth(aLong));
                            networkRequest(netApi.my_income_1(DateUtils.getMonthBegin(aLong),
                                    DateUtils.getMonthEnd(aLong)), this, 999);
                            return null;
                        }).build();
            }
            datePickerDialog.show();
        } else if (view.getId() == R.id.tv_order_details) {
            String endTime, startTime;
            long day = 86400000;
            endTime = DateUtils.getYearMonthDay(currentTimestamp);
            if (mRbNearlyDay7.isChecked()) {
                startTime = DateUtils.getYearMonthDay(currentTimestamp - day * 7);
            } else {
                startTime = DateUtils.getYearMonthDay(currentTimestamp - day * 30);
            }
            startActivity(new Intent(mActivity, OrderSearchActivity.class)
                    .putExtra("startTime", startTime).putExtra("endTime", endTime));
        } else if (view.getId() == R.id.ll_have_already_settled) {
            startActivity(new Intent(mActivity, RevenueDetailActivity.class)
                    .putExtra("type", 0)
                    .putExtra("theSameMonth", theSameMonth));
        } else if (view.getId() == R.id.ll_unsettled) {
            startActivity(new Intent(mActivity, RevenueDetailActivity.class)
                    .putExtra("type", 1)
                    .putExtra("theSameMonth", theSameMonth));
        }
    }

    @Override
    public void onResponse(int requestCode, String json, Response<ResponseBody> response) throws Exception {
        loadingPopupDismiss();
        RevenueBean bean = new Gson().fromJson(json, RevenueBean.class);
        if (requestCode == 999) {
            if (bean.getCode().equals("1")) {
                mTvHaveAlreadySettled.setText(String.format("￥%s", bean.getData().getSettlement_money()));
                mTvUnliquidated.setText(String.format("￥%s", bean.getData().getOnway_money()));
                mTvGeneralIncome.setText(String.valueOf(bean.getData().getTotal_money()));
            }
        } else {
            if (bean.getCode().equals("1")) {
                mTvAccomplish.setText(String.format("%s单", bean.getData().getComplete_num()));
                mTvUnfinished.setText(String.format("%s单", bean.getData().getOnway_num()));
            }
        }
    }

    @Override
    public void onFailure(int requestCode) {
        loadingPopupDismiss();
    }
}